using Microsoft.AspNetCore.Builder;
using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System.IO;
using System.Linq;
using System.Reflection;
using System.ComponentModel;
using util.EnumExtion;

namespace util.APPExtension
{
    public static class SwaggerBuilder
    {
        public static IApplicationBuilder UseMySwagger(this IApplicationBuilder app)
        {
            app.UseSwagger();
            app.UseSwaggerUI(c =>
             {
                 var apiGroupNames = Enum.GetValues(typeof(ApiGroupNameEnum));
                 foreach (var item in apiGroupNames)
                 {
                     var index = Convert.ToInt32(item);
                     var value = item.ToString();
                     var des = item.GetType().GetField(item.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), true)[0] as DescriptionAttribute;
                     c.SwaggerEndpoint($"/swagger/{value}/swagger.json", des.Description);
                 }
             });
            return app;
        }
    }
}